/*
 * 导出通讯录
 */
package address;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import javax.swing.*;

/**
 *
 * @author Mavis
 */
public class ExportCon extends JFrame {

    //通讯录联系人信息的线性表
    ArrayList contacts = new ArrayList();
    Font f = new Font("微软雅黑", Font.PLAIN,18);
    Font f2 = new Font("微软雅黑", Font.PLAIN,16);
    Font f3 = new Font("微软雅黑", Font.PLAIN, 12);
    JLabel label1 = new JLabel("* 选择导出通讯录的格式：");
    JLabel label2 = new JLabel("* 选择导出通讯录的路径：");

    public ExportCon(ArrayList contacts) {
        setLayout(null);
        this.contacts = contacts;
        exportPanel p = new exportPanel();
        add(p);
        p.setFocusable(true);
        p.setBounds(0, 0, 300, 300);
    }

    private class exportPanel extends JPanel {

        //导出面板
        JPanel exportpanel = new JPanel();
        //导出格式选择面板
        JPanel exportStylePanel = new JPanel();
        //导出格式vCard的单选按钮
        JRadioButton jrbvCard = new JRadioButton(" vCard ");
        //导出格式CSV的单选按钮
        JRadioButton jrbCSV = new JRadioButton(" CSV ");
        //导出通讯录路径的选择按钮
        JButton jbtSelectPath = new JButton("选择路径");
     
        //导出通讯录的目录
        String Path = null;

        public exportPanel() {
            setLayout(null);
            exportpanel.setLayout(null);
            //设置导出面板的大小
           exportpanel.setBounds(0, 0, 300, 300);
            add(exportpanel);
            //设置导出面板背景为白色
            exportpanel.setBackground(Color.white);

            //设置label1的参数
            label1.setBounds(10, 10, 200, 30);
            label1.setFont(f3);
            label1.setOpaque(false);
            exportpanel.add(label1);

            //设置导出格式面板的参数
            exportStylePanel.setBounds(20, 45, 250, 70);
            exportStylePanel.setOpaque(false);
            exportStylePanel.setFocusable(true);
            exportStylePanel.setLayout(null);
            exportpanel.add(exportStylePanel);

            //设置导出格式单选按钮的参数
            jrbvCard.setBounds(0, 0, 100, 30);
            jrbvCard.setOpaque(false);
            jrbvCard.setFont(f);
                        
            jrbCSV.setBounds(140,0, 100, 30);
            jrbCSV.setOpaque(false);
            jrbCSV.setFont(f);
                       
            ButtonGroup group = new ButtonGroup();
            group.add(jrbvCard);
            group.add(jrbCSV);
            exportStylePanel.add(jrbvCard);
            exportStylePanel.add(jrbCSV);
            //设置label2的参数
            label2.setBounds(10, 80, 250, 30);
            label2.setOpaque(false);
            label2.setFont(f3);
            exportpanel.add(label2);

            //设置jbtSelectPath的参数
            jbtSelectPath.setBounds(50, 122, 180, 50);
            jbtSelectPath.setOpaque(false);
            jbtSelectPath.setFont(f);
            jbtSelectPath.setBackground(Color.white);
            exportpanel.add(jbtSelectPath);
            
            jbtSelectPath.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    File f;
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                        f = chooser.getSelectedFile();
                        Path = f.getPath();
                    }
                }

            });

            //设置确认按钮的参数
            ImageIcon Icon1 = new ImageIcon("img/confirm.png");   
            JButton jbtOK = new JButton(Icon1);   
            jbtOK.setBorderPainted(false);                  
            jbtOK.setBounds(25, 200, 100, 30);
            exportpanel.add(jbtOK);
            jbtOK.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    File f;
                    if (jrbvCard.isSelected() || jrbCSV.isSelected()) {
                        if (Path != null) {
                            if (jrbvCard.isSelected()) {
                                f = new File(Path+"/contacts.vCard");
                            } else {
                                f = new File( Path+"/contacts.CSV");
                            }
                            RandomAccessFile inout = null;
                            try {
                                inout = new RandomAccessFile(f, "rw");
                                try {
                                    inout.setLength(0);
                                } catch (IOException ex) {
                                    System.out.println("11");
                                }
                                try {
                                    inout.close();
                                } catch (IOException ex) {
                                    System.out.println("11");
                                }

                            } catch (FileNotFoundException ex) {
                                System.out.println("11");
                            }

                            ObjectOutputStream output = null;

                            try {
                                output = new ObjectOutputStream(new FileOutputStream(f, true));
                            } catch (IOException ex) {
                                System.out.println("11");
                            }
                            for (int i = 0; i <contacts.size(); i++) {
                                try {
                                    output.writeObject((People) contacts.get(i));
                                } catch (IOException ex) {
                                    System.out.println("11");
                                }
                            }
                            JOptionPane.showMessageDialog(null, "成功导出通讯录！", "注意", JOptionPane.INFORMATION_MESSAGE);
                           dispose();
                        } else {
                            JOptionPane.showMessageDialog(null, "请选择导出通讯录的路径", "注意", JOptionPane.INFORMATION_MESSAGE);

                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "请选择导出通讯录的格式", "注意", JOptionPane.INFORMATION_MESSAGE);
                    }

                }

            });

            //设置取消按钮的参数
            ImageIcon Icon2 = new ImageIcon("img/cancel_newPeople.png");   
            JButton jbtCancel = new JButton(Icon2);   
            jbtCancel.setBorderPainted(false);  
            jbtCancel.setBounds(155, 200, 100, 30);
            exportpanel.add(jbtCancel);
            jbtCancel.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
                    dispose();
                }

            });
        }
    }
}
